<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <script src="../js/轮播图.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .wrap {
            width: 1000px;
            height: 500px;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
        }

        .wrap .navBar {
            display: flex;
            position: absolute;
            right: 10px;
            bottom: 10px;
            z-index: 999;
        }

        .wrap .navBar li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            font-size: 0;
            border: 2px solid #333;
            margin: 0 5px;
        }

        .wrap .navBar .active {
            background-color: #6ff;
            color: #fff;
        }

        .main {
            /* position: relative; */
            /* height: 400px; */
            width: max-content;
            position: absolute;
            left: 0;
            top: 0;
        }

        .main>div {
            width: 1000px;
            height: 500px;
            /* opacity: 0; */
            float: left;

        }

        .main img {
            width: 100%;
            height: 100%;
            vertical-align: top;
            transition: all 0.5s;
        }

        .btn {
            position: absolute;
            right: 0px;
            bottom: -40px;
        }

        .left,
        .right {
            width: 40px;
            height: 50px;
            background-color: rgba(0, 0, 0, 0.5);
            font-size: 30px;
            line-height: 50px;
            text-align: center;
            color: #fff;
            cursor: context-menu;
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto 0;
            z-index: 3;
        }

        .right {
            right: 0;
        }

        .left:hover,
        .right:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="navBar">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="main">
            <div class="show">
                <img src="../images/1.jpg" alt="">
            </div>
            <div>
                <img src="../images/2.jpg" alt="">
            </div>
            <div>
                <img src="../images/3.webp" alt="">
            </div>
            <div>
                <img src="../images/4.webp" alt="">
            </div>
            <div class="show">
                <img src="../images/1.jpg" alt="">
            </div>
        </div>
        <!-- <button class="btn">去留言</button> -->
        <div class="left"><span>&lt;</span></div>
        <div class="right"><span>&gt;</span></div>
    </div>


</body>
<script>
    window.onload = function () {
        var navBar = document.getElementsByClassName("navBar")[0];
        var liList = navBar.getElementsByTagName("li");
        var main = document.getElementsByClassName("main")[0];
        var divList = main.getElementsByTagName("div");
        var wrap = document.getElementsByClassName("wrap")[0];
        var btn = document.getElementsByClassName("btn")[0];
        var left = document.getElementsByClassName("left")[0];
        var right = document.getElementsByClassName("right")[0];

        autoPlay();

        var swiperWidth = divList[0].clientWidth;

        var timer, index = 0;
        for (let i = 0; i < liList.length; i++) {
            let list = liList[i];
            list.onclick = function (e) {

                index = i;
                // for (var j = 0; j < liList.length; j++) {
                //     liList[j].className = "";
                //     // divList[j].className = "";
                // }
                // this.className = "active";
                // // divList[i].className = "show";
                // animate(main, "left", -swiperWidth * i, "slow");
                move();

            }
        }

        left.onclick = function () {
            if(main.isAni) return false;
            index--;
            move();
        }

        right.onclick = function () {
            index++;
            move();
        }

        //移入暂停
        wrap.onmouseenter = function () {
            clearInterval(timer);
        }

        // 移出启动
        wrap.onmouseleave = function () {
            autoPlay();
        }

        //窗口失焦
        window.onblur = function() {
            clearInterval(timer);
        }

        //窗口获焦
        window.onfocus = function() {
            autoPlay();
        }

        // 自动轮播
        function autoPlay() {
            // clearInterval(timer);
            timer = setInterval(function () {
                index++;
                move();
            }, 3000);
        }

        function move() {
            // 左键点击处理
            if (index < 0) {
                main.style.left = -swiperWidth * (liList.length) + "px";
                index = liList.length;
                index--;
            }

            // 右键点击处理
            if (index > liList.length) {
                main.style.left = 0;
                index = 0;
                index++;
            }

            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
            }
            // index >= 4 ? 0 : index  判断如果是第五张 把第一张改为活跃状态
            liList[index >= liList.length ? 0 : index].className = "active";

            animate(main, "left", -swiperWidth * index, "slow", function () {
                // 如何实现滚动切换  (障眼法)
                // 每次运动结束之后  判断是否是第五张, 如果是第五张 立即切换到第一张
                // 第四张  滚动 第五张 切换到  第一张
                if (index >= liList.length) {
                    main.style.left = 0;
                    index = 0;
                }
            });
        }

    }

</script>

</html>